/*
This file creates the tables for Sandvik, Sauoma, Seegert, Stanton (2020)


Inputs
	Data "DataforAnalysis.dta" merged in file MergeData.do
				
Outputs
	Tables
		1) Table 1 descriptive statistics 
		2) Table 2 baseline analysis
		3) Table 3 persistence
		4) Table 4 highperformer partner 
		5) Table 5 worksheet Analysis
 
*/

clear
set more off

*set paths 
	cd  
	global datapath  
	global tablepath  
	global figurepath  
    global logpath

	use "$datapath\DataforAnalysis.dta", clear
	 
	cap log close
	log using "$logpath\produceTables.smcl", replace

*Save tests 
	matrix pval = J(4,11,0)	
	matrix individuals = J(4,6,0)
	matrix ind_treat = J(5,6,0)
	matrix ind_treat_persist = J(5,6,0)
	matrix man_treat = J(5,6,0)
	matrix man_treat_persist = J(5,6,0)
	matrix obs_treat = J(5,6,0)
	matrix obs_treat_persist = J(5,6,0)
	
*------------------------------------------------------------------------------*
*                           Table samples 
*------------------------------------------------------------------------------*
*Table 2 baseline analysis
	local baselinesample1   "if Week > -4 & Week < 5 & Treatment !=5"
	local baselinesample2   "if Week > -4 & Week < 5 & Treatment !=1"
	local baselinesample3   "if Week > -4 & Week < 5"
	local baselinesample4   "if Week > -4 & Week < 5"
	
*Table 3 persistence
	local persistencesample1   "if (Week >= -3 & Week <= 0 & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment !=5)  "
	local persistencesample2   "if (Week >= -3 & Week <= 0 & Treatment !=1) | (Week >= 5 & Week <= 24 & Treatment !=1) "
	local persistencesample3   "if (Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24) "
	local persistencesample4   "if (Week >= -3 & Week <= 0 & balancesample ==1) | (Week >= 5 & Week <= 24 & balancesample ==1)    "
	local persistencesample5   "if (Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24) "
		
*Table 4 high and non high person subsamples
	local highsample1 `baselinesample1'
	local highsample2 "if Week > -4 & Week < 5 & Treatment !=5 & highperformer == 0"
	local highsample3 "if Week > -4 & Week < 5 & Treatment !=5 & highperformer == 1"
	local highsample4 `persistencesample1'
	local highsample5 "if (Week >= -3 & Week <= 0  & Treatment !=5 & highperformer == 0) | (Week >= 5 & Week <= 24  & Treatment !=5 & highperformer == 0) "
	local highsample6 "if (Week >= -3 & Week <= 0 & Treatment !=5 & highperformer == 1 ) | (Week >= 5 & Week <= 24 & Treatment !=5 & highperformer == 1 ) "
	
*Table 5 knowledge samples
	local knowledgesample1 "if worksheetsample == 1 & (Knowledge_Received_Any == 1 | Support_Received_Any == 1)"
	local knowledgesample2 "if worksheetsample == 1 & (Knowledge_alt_Received_Any == 1 | Support_alt_Received_Any == 1) "
	local knowledgesample3 "if worksheetsample == 1 & (Knowledge_Y_Received_Any == 1 | Support_Y_Received_Any == 1)"
	
*------------------------------------------------------------------------------*
*                  Table count individuals and managers 
*------------------------------------------------------------------------------* 

* First assert that there are no missing values--which validates this approach.
foreach i of varlist pairincentive structuredmeeting combined pairincentivepost structuredmeetingpost combinedpost manager ID Week {
	assert `i'!=.
}

    *Table 3 Baseline 
	forvalues i = 1(1)4 {
		forvalues j = 1(1)5 {
			*Individuals by treat
				gen insamp = 1 `baselinesample`i'' & Treatment ==`j'
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat[`j',`i'] =  r(N)
				drop exist insamp
				
			 *Managers by treat
				gen insamp = 1     `baselinesample`i'' & Treatment ==`j'
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat[`j',`i'] =  r(N)
				drop exist insamp
				
			*obs by treat
				 count  `baselinesample`i'' & Treatment ==`j'
				 matrix obs_treat[`j',`i'] =  r(N)
			 }
			 
			*Individuals
				gen insamp = 1 `baselinesample`i''
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix individuals[1,`i'] =  r(N)
				drop exist insamp	
				
			*Managers
				gen insamp = 1     `baselinesample`i''
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix individuals[2,`i'] =  r(N)
				drop exist insamp
	}

 	*Individuals by treat
		*persistence sample 1
			forvalues j = 1(1)5 {
				gen insamp = 1 if (Week >= -3 & Week <= 0 & Treatment ==`j' & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment ==`j' & Treatment !=5)  
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat_persist[`j',1] =  r(N)
				drop exist insamp
				
			*Managers by treat
				gen insamp = 1  if   (Week >= -3 & Week <= 0 & Treatment ==`j' & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment ==`j' & Treatment !=5)    
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat_persist[`j',1] =  r(N)
				drop exist insamp
				
			*obs by treatment
				 count  if (Week >= -3 & Week <= 0 & Treatment ==`j' & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment ==`j' & Treatment !=5)  
				 matrix obs_treat_persist[`j',1] =  r(N)
				
			 }
		*persistence sample 2
			forvalues j = 1(1)5 {
				gen insamp = 1 if (Week >= -3 & Week <= 0 & Treatment !=1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment !=1 & Treatment ==`j')  
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat_persist[`j',2] =  r(N)
				 drop exist insamp
				
			*Managers by treat
				gen insamp = 1  if   (Week >= -3 & Week <= 0 & Treatment !=1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment !=1 & Treatment ==`j')   
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat_persist[`j',2] =  r(N)
				drop exist insamp
				
			*obs by treatment
				 count  if (Week >= -3 & Week <= 0 & Treatment !=1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment !=1 & Treatment ==`j')  
				 matrix obs_treat_persist[`j',2] =  r(N) 
				 
			 }
		*persistence sample 3
			forvalues j = 1(1)5 {
				gen insamp = 1 if (Week >= -3 & Week <= 0 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment ==`j')   
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat_persist[`j',3] =  r(N)
				 drop exist insamp
				 
			*Managers by treat
				gen insamp = 1 if   (Week >= -3 & Week <= 0 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment ==`j')
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat_persist[`j',3] =  r(N)
				drop exist insamp 
				
			*obs by treatment
				 count  if (Week >= -3 & Week <= 0 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment ==`j')
				 matrix obs_treat_persist[`j',3] =  r(N)  
				 
			 }
		*persistence sample 4
			forvalues j = 1(1)5 {
				gen insamp = 1 if (Week >= -3 & Week <= 0 & balancesample ==1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & balancesample ==1 & Treatment ==`j') 
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat_persist[`j',4] =  r(N)
				 drop exist insamp
				 
			*Managers by treat
				gen insamp = 1  if (Week >= -3 & Week <= 0 & balancesample ==1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & balancesample ==1   & Treatment ==`j')
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat_persist[`j',4] =  r(N)
				drop exist insamp  
				
			*obs by treatment
				 count  if (Week >= -3 & Week <= 0 & balancesample ==1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & balancesample ==1 & Treatment ==`j')
				 matrix obs_treat_persist[`j',4] =  r(N)   
				 
			 }	 
		*persistence sample 5
			forvalues j = 1(1)5 {
				gen insamp = 1 if (Week >= -3 & Week <= 0 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment ==`j') 
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat_persist[`j',5] =  r(N)
				 drop exist insamp
				 
			*Managers by treat
				gen insamp = 1  if (Week >= -3 & Week <= 0 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment ==`j') 
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat_persist[`j',5] =  r(N)
				drop exist insamp  
				
			*obs by treatment
				 count  if (Week >= -3 & Week <= 0 & Treatment ==`j') | (Week >= 5 & Week <= 24 & Treatment ==`j') 
				 matrix obs_treat_persist[`j',5] =  r(N)   
			 }	 
		*persistence sample 6
			forvalues j = 1(1)5 {
				gen insamp = 1 if (Week >= -3 & Week <= 0 & uniqueManagerWeek ==1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & uniqueManagerWeek ==1 & Treatment ==`j') 
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix ind_treat_persist[`j',6] =  r(N)
				drop exist insamp
				 
			*Managers by treat
				gen insamp = 1 if (Week >= -3 & Week <= 0 & uniqueManagerWeek ==1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & uniqueManagerWeek ==1 & Treatment ==`j') 
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix man_treat_persist[`j',6] =  r(N)
				drop exist insamp 
				
			*obs by treatment
				 count  if (Week >= -3 & Week <= 0 & uniqueManagerWeek ==1 & Treatment ==`j') | (Week >= 5 & Week <= 24 & uniqueManagerWeek ==1 & Treatment ==`j') 
				 matrix obs_treat_persist[`j',6] =  r(N) 
				 
			 }		 


 	*Table 4 Persistence Table
	forvalues i = 1(1)6 {
			*Individuals
				gen insamp = 1 `persistencesample`i''  
				bysort ID insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix individuals[3,`i'] =  r(N)
				drop exist insamp
			*Managers
				gen insamp = 1    `persistencesample`i''
				bysort manager insamp: gen exist = 1 if _n ==1
				sum exist if insamp == 1
				matrix individuals[4,`i'] =  r(N)
				drop exist insamp
	}

	mat li individuals 
	mat li ind_treat
	mat li ind_treat_persist
	
	mat li man_treat
	mat li obs_treat
	mat li obs_treat_persist
	
*------------------------------------------------------------------------------*
*                          Produce tables
*------------------------------------------------------------------------------* 

*------------------------------------------------------------------------------*
*       Table 1 descriptive statistics
*------------------------------------------------------------------------------*
 preserve
	sort ID Week

*determine teammates  
	gen  period = ( Week < 5 & Week > 0)
	bysort manager ID  period: gen uniqueAgentNum = 1 if _n ==1  

	sum uniqueAgentNum if  period==1
	bysort manager: egen totalAgents = total(uniqueAgentNum) if  period ==1
	
	sum totalAgents  [fw = uniqueAgentNum]
	sum totalAgents  [fw = uniqueAgentNum] if Treatment ==1 , d   
	sum totalAgents [fw = uniqueAgentNum]  if Treatment ==2   , d  
	sum totalAgents [fw = uniqueAgentNum]  if Treatment ==3   , d 
	sum totalAgents [fw = uniqueAgentNum]  if Treatment ==4, d
	sum totalAgents [fw = uniqueAgentNum]  if Treatment ==5, d
 
	bysort manager  period: gen uniqueManager2 = 1 if _n ==1 
	reg totalAgents i.Treatment if Treatment !=5 & uniqueManager2 ==1
	test   2.Treatment = 3.Treatment = 4.Treatment
	
*eligible agents
	bysort location brand Treatment Week: egen NumPossiblePartners = count(ID) 
 
	sum NumPossiblePartners   if Treatment !=5
	sum NumPossiblePartners   if Treatment ==1  
	sum NumPossiblePartners   if Treatment ==2     
	sum NumPossiblePartners   if Treatment ==3    
	sum NumPossiblePartners   if Treatment ==4 
	bys manager: gen NumTeammates = _N

*generate averages by individual	
	local descriptiveStatistics age tenure female lrpc lrph commission calls phonehours adherence  
	foreach var in `descriptiveStatistics' {
		bys ID: egen avg_`var' = mean(`var')  if Week < 1 & Week > -4 
		bys ID: egen Avg_`var' = max(avg_`var')
	}
	keep if Week<=0

	foreach var in `descriptiveStatistics' {
		by ID: egen Checkavg_`var' = mean(`var')  if Week < 1 & Week > -4 
		
	}


*keep only one observation per individual	
	duplicates drop ID, force
	
*generate index to fill in descriptive statistics	
	gen counter = _n
	replace counter = . if counter > 44
	
*generate columns (stored in y`i') 
	gen column1 = 1
	gen column2 = (Treatment == 3)
	gen column3 = (Treatment == 2)
	gen column4 = (Treatment == 4)
	gen column5 = (Treatment == 1)	
	gen column6 = (Treatment == 5)

*calculate descriptive statistics for each column case
	forvalues i = 1(1)6 {
	gen y`i' = .	
	local k = 2
	foreach var in `descriptiveStatistics'   {
	
		if `k' == 10 {
			sum Avg_`var' if column`i' == 1, d 
			replace y`i' = round(r(mean),.01) if counter == `k'
			local k = `k'+2
		}
		else {
			sum Avg_`var' if column`i' == 1, d 
			replace y`i' = round(r(mean),.01) if counter == `k'
			local k = `k'+1
			replace y`i' = round(r(p50),.01) if counter == `k'
			local k = `k'+1
			replace y`i' = round(r(sd),.01) if counter == `k'
			local k = `k'+2
		}
	}
	*Attrition
		sum term if term == 1 & column`i' == 1
			replace y`i' = (r(N)/736) if column1 == 1 & counter == 44
			replace y`i' = (r(N)/158) if column2 == 1 & counter == 44
			replace y`i' = (r(N)/135) if column3 == 1 & counter == 44
			replace y`i' = (r(N)/174) if column4 == 1 & counter == 44
			replace y`i' = (r(N)/186) if column5 == 1 & counter == 44
			replace y`i' = (r(N)/83)  if column6 == 1 & counter == 44
			
	*Number of agents
		sum RPC if column`i' == 1, d
		replace y`i' = r(N) if counter == 46
	}  

*column 7 (stored in y7) reports joint test of equality
	gen y7 = .
	
	*create indicator for treatment	
	tab Treatment, gen(tgroup)
	
	*loop through descriptive statistics 
	local k = 2
	foreach var in `descriptiveStatistics'   {
		regress Avg_`var' tgroup1 tgroup2 tgroup3 tgroup4, vce(cluster manager) noconstant 
		test tgroup1 = tgroup2 = tgroup3 = tgroup4
		replace y7 = r(p) in `k'
		if `k' == 10 {
			local k = `k' +2	// no standard error or median on female
		}
		else {
			local k = `k' +4
		}
	}

*generate rows in the table		
	forvalues i = 1(1)36 {
		gen d`i' = (counter==`i')
	}

*label rows in the table
	label var d1  "Age (yrs.)"
	label var d2  "\hspace{4mm} Mean" 
	label var d3  "\hspace{4mm} Median"
	label var d4  "\hspace{4mm} Std Dev."
	label var d5  "Tenure (log days)" 
	label var d6  "\hspace{4mm} Mean" 
	label var d7  "\hspace{4mm} Median" 
	label var d8  "\hspace{4mm} Std Dev."
	label var d9  "Percent female" 
	label var d10 "\hspace{4mm} Mean" 
	label var d11 "Revenue per Call (log)"
	label var d12 "\hspace{4mm} Mean"	
	label var d13 "\hspace{4mm} Median"
	label var d14 "\hspace{4mm} Std Dev."
	label var d15 "Revenue per Hour (log)"
	label var d16 "\hspace{4mm} Mean"
	label var d17 "\hspace{4mm} Median"	
	label var d18 "\hspace{4mm} Std Dev."
	label var d19 "Commission"
	label var d20 "\hspace{4mm} Mean"
	label var d21 "\hspace{4mm} Median"	
	label var d22 "\hspace{4mm} Std Dev."	
	label var d23 "Total calls"
	label var d24 "\hspace{4mm} Mean"
	label var d25 "\hspace{4mm} Median"	
	label var d26 "\hspace{4mm} Std Dev."	
	label var d27 "Phone Hours"
	label var d28 "\hspace{4mm} Mean"
	label var d29 "\hspace{4mm} Median"	
	label var d30 "\hspace{4mm} Std Dev."	
	label var d31 "Adherence"
	label var d32 "\hspace{4mm} Mean"
	label var d33 "\hspace{4mm} Median"	
	label var d34 "\hspace{4mm} Std Dev."
	label var d36 "N agents"
	
	local outreg_opt = "replace"		//replace if first time, then append
	forvalues r=1(1)7 {
		qui reg y`r' d1-d36, nocons noheader
		outreg2 d1-d36 using "$tablepath\Table1.tex", nose label noaster tex  dec(2) ctitle(`ctitle`r'') `outreg_opt'	 
		local outreg_opt = "append" 		//replace if first time, then append
	}	

	drop counter column* d1-d36 y1-y7	// drop unnecessary variables moving forward

restore   //to make this table had to drop observations, return to previous data before that

*------------------------------------------------------------------------------*
** Table 2 Revenue Treatment Effects in the Intervention Period
*------------------------------------------------------------------------------*

*Internal control 
	reghdfe lrpc     pairincentive structuredmeeting combined     `baselinesample1' , absorb(manager Week) vce(cluster manager)	
	est store A
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,1] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,1] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,1] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,1] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*External control 
	reghdfe lrpc     pairincentive structuredmeeting combined     `baselinesample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,2] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,2] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,2] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,2] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Both internal and external control 
	reghdfe lrpc pairincentive structuredmeeting combined   internalcontrol     `baselinesample3'   , absorb(manager Week) vce(cluster manager)	
	est store C
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,3] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,3] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,3] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,3] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*Both internal and external control individual fixed effects
	reghdfe lrpc     pairincentive structuredmeeting combined   internalcontrol   `baselinesample4' , absorb(ID Week) vce(cluster manager)	keepsingle
	est store D
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,4] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,4] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,4] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,4] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Total revenue Both internal and external control individual fixed effects
	reghdfe revenue pairincentive structuredmeeting combined   internalcontrol   `baselinesample4' , absorb(manager Week) vce(cluster manager)	keepsingle
	est store E
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,5] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,5] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,5] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,5] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 

estout1 A B C D E    using "$tablepath\Table2.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeeting  pairincentive  combined internalcontrol) 

*------------------------------------------------------------------------------*
*  Table 3 Treatment Effect Persistence in the Post-Intervention Period
*------------------------------------------------------------------------------*
  
*Internal control 
	reghdfe lrpc pairincentivepost structuredmeetingpost combinedpost     `persistencesample1' , absorb(manager Week) vce(cluster manager)	
	est store A
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,6] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,6] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,6] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,6] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*External control 
	reghdfe lrpc   pairincentivepost structuredmeetingpost combinedpost    `persistencesample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,7] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,7] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,7] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,7] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*Both internal and external control 
	reghdfe lrpc internalcontrolpost pairincentivepost structuredmeetingpost combinedpost     `persistencesample3'   , absorb(manager Week) vce(cluster manager)	
	est store C
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,8] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,8] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,8] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,8] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*Balanced
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `persistencesample4' , absorb(manager Week) vce(cluster manager)	
	est store D
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,9] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,9] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,9] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,9] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Both internal and external control individual fixed effects
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `persistencesample5' , absorb(ID Week) vce(cluster manager) keepsingle	
	est store E
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,10] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,10] = round(r(p),0.001) 
		test pairincentivepost = combinedpost
		matrix pval[3,10] = round(r(p),0.001)  
		test combined   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,10] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Total Revenue Both internal and external control 
	reghdfe revenue internalcontrolpost pairincentivepost structuredmeetingpost combinedpost     `persistencesample3'   , absorb(manager Week) vce(cluster manager)	
	est store F
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,11] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,11] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,11] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,11] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  

 estout1 A B C D E F   using "$tablepath\Table3.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeetingpost  pairincentivepost  combinedpost internalcontrolpost) 

*------------------------------------------------------------------------------*
*     Table 4 Revenue-per-Call Treatment Effect Heterogeneity 
*          by Partner and Agent Performance 
*------------------------------------------------------------------------------*

*generate independent variables in intervention and post period
	local independent_intervention  pairincentive structuredmeeting combined pairincentive_high structuredmeeting_high combined_high highperformerpartner highperformerpartner_ever 
	local independent_post pairincentivepost structuredmeetingpost combinedpost pairincentive_post_high structuredmeeting_post_high combined_post_high highperformerpartner hp_post 
	
*Baseline estimate internal control
	reghdfe RPC  `independent_intervention' `highsample1' , absorb(manager Week) vce(cluster manager)	
	est store A
	
*Non-high performer person subsample
	reghdfe RPC  `independent_intervention' `highsample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
*High performer person subsample	
	reghdfe RPC `independent_intervention'  `highsample3', absorb(manager Week) vce(cluster manager)	
	est store C
	
*Post period
	reghdfe RPC  `independent_post'         `highsample4' , absorb(manager Week) vce(cluster manager)	
	est store D	
	
*Post period Non-high performer person subsample
	reghdfe RPC  `independent_post'          `highsample5' , absorb(manager Week) vce(cluster manager)	
	est store E	
	
*Post period High performer person subsample
	reghdfe RPC  `independent_post'          `highsample6' , absorb(manager Week) vce(cluster manager)	
	est store F	
	
estout1 A B C D  E F  using "$tablepath\Table4.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeeting_high pairincentive_high combined_high structuredmeeting  pairincentive  combined  structuredmeeting_post_high pairincentive_post_high combined_post_high  structuredmeetingpost  pairincentivepost  combinedpost ) 

*------------------------------------------------------------------------------*
* Table 5 Post-Intervention Correlations Between Sales Revenue 
*          and Knowledge Documented in Worksheets
*------------------------------------------------------------------------------*
 
 *word repsence classification
	*define variables
		gen tbl_knowledge_post = Knowledge_Received_Any_Post
		gen tbl_knowledge = Knowledge_Received_Any
		gen tbl_mixedr_post = Mixed_Received_Any_Post
		gen tbl_mixedr = Mixed_Received_Any
		
	*label variables
		label var tbl_knowledge_post 	"Received Knowledge $\times$ Post-Period"
		label var tbl_mixedr_post		"Received Mixed Advice $\times$ Post-Period"

		
	*log revenue per call word presence classification		
		reghdfe lrpc tbl_knowledge_post  tbl_knowledge	`knowledgesample1' , absorb(manager Week) vce(cluster manager)	
		est store A

*Word and manual classification
	*redefine variables
		replace tbl_knowledge_post = Knowledge_alt_Received_Any_Post
		replace tbl_knowledge = Knowledge_alt_Received_Any
		replace tbl_mixedr_post = Mixed_alt_Received_Any_Post
		replace tbl_mixedr = Mixed_alt_Received_Any 
		
	*log revenue per call word and manual		
		reghdfe lrpc   tbl_knowledge_post	tbl_knowledge	`knowledgesample2' , absorb(manager Week) vce(cluster manager)	
			est store B
			
	*log revenue per call word and manual with knowledge and support	
		reghdfe lrpc  tbl_knowledge_post	 tbl_mixedr_post		  	`knowledgesample2' , absorb(ID Week) vce(cluster manager)	
			est store C		
		
*Blinded classification		
	*redefine variables
		replace tbl_knowledge_post = Knowledge_Y_Received_Any_Post
		replace tbl_knowledge = Knowledge_Y_Received_Any	
		replace tbl_mixedr_post = Mixed_Y_Received_Any_Post	
		replace tbl_mixedr = Mixed_Y_Received_Any 	
		
	*Log revenue per call blinded		
		reghdfe lrpc tbl_knowledge_post	 tbl_mixedr_post  tbl_knowledge    tbl_mixedr	`knowledgesample3' , absorb(manager Week) vce(cluster manager)	
		est store D			
		
*Word and manual classification
	*redefine variables
		replace tbl_knowledge_post = Knowledge_alt_Received_Any_Post
		replace tbl_knowledge = Knowledge_alt_Received_Any
		replace tbl_mixedr_post = Mixed_alt_Received_Any_Post
		replace tbl_mixedr = Mixed_alt_Received_Any 
		
	*Revenue word and manual
		reghdfe Revenue tbl_knowledge_post	 tbl_mixedr_post  tbl_knowledge    tbl_mixedr	`knowledgesample2'  , absorb(manager Week) vce(cluster manager)	
			est store E
			
*Blinded classification		
	*redefine variables
		replace tbl_knowledge_post = Knowledge_Y_Received_Any_Post
		replace tbl_knowledge = Knowledge_Y_Received_Any	
		replace tbl_mixedr_post = Mixed_Y_Received_Any_Post	
		replace tbl_mixedr = Mixed_Y_Received_Any 	
		
	*Revenue blinded
		reghdfe Revenue tbl_knowledge_post	 tbl_mixedr_post  tbl_knowledge    tbl_mixedr	`knowledgesample3'  , absorb(manager Week) vce(cluster manager)	
			est store F
		
	estout1 A B C D E F using "$tablepath\Table5.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(tbl_knowledge_post tbl_mixedr_post ) 
 


log close